package com.rulin.asp.business.datacleaning.controller;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.rulin.asp.business.datacleaning.entity.QuestionNew;
import com.rulin.asp.business.datacleaning.service.DataCleanService;
import com.rulin.asp.business.service.DictionaryService;

@Controller
@RequestMapping("/dataCleaning")
public class DataCleanController {

	@Autowired
	private DataCleanService dataCleanService;
	
	@Autowired
	private DictionaryService dictionaryService;
	
	@RequestMapping(value = "/draft", method = RequestMethod.GET)
	public ModelAndView draft(ModelAndView view){
		String id = UUID.randomUUID().toString();
		
		view.addObject("id",id);
		view.setViewName("redirect:/dataCleaning/editPage.html");
		return view;
	}
	
	@RequestMapping(value = "/editPage.html", method = RequestMethod.GET)
	public ModelAndView editPage(ModelAndView view,String id){
		setDiv(view);
		
		QuestionNew questionNew = dataCleanService.getQuestion(id);
		view.addObject("form", questionNew);
		view.setViewName("/business/datacleaning/data_edit");
		return view;
	}
	
	@RequestMapping(value = "/readPage.html", method = RequestMethod.GET)
	public ModelAndView readPage(ModelAndView view,String id){
		setDiv(view);
		
		QuestionNew questionNew = dataCleanService.getQuestion(id);
		view.addObject("form", questionNew);
		view.setViewName("/business/datacleaning/data_read");
		return view;
	}
	
	public void setDiv(ModelAndView model){
		model.addObject("questionSources", dictionaryService.getByModel("question_source"));
		model.addObject("majors", dictionaryService.getByModel("clean_major_type"));
		model.addObject("subjects", dictionaryService.getByModel("clean_subject_type"));
	}
	
	@RequestMapping(value = "/saveQuestion.json", method = RequestMethod.POST)
	public void saveQuestion(QuestionNew data) {
		dataCleanService.saveData(data);
	}
}
